package com.whiteape.book.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.whiteape.book.R;
import com.whiteape.book.contract.RecommendContract;
import com.whiteape.book.model.zssq.BookListDetailBean;
import com.whiteape.book.model.zssq.BookSubSortBean;
import com.whiteape.book.model.zssq.section.BookListDetailSection;
import com.whiteape.book.presenter.RecommendPresenter;
import com.whiteape.book.ui.activity.BookCategoryActivity;
import com.whiteape.book.ui.activity.BookCategoryDetailActivity;
import com.whiteape.book.ui.activity.BookRecommendDetailActivity;
import com.whiteape.book.ui.activity.SearchActivity;
import com.whiteape.book.ui.adapter.RecommendChildAdapter;
import com.whiteape.book.ui.base.BaseFragment;
import com.whiteape.book.ui.base.BaseMVPFragment;
import com.whiteape.book.widget.itemdecoration.DividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 推荐fragment的子fragment
 * 主要是一些书单内容
 */
public class RecommendChildFragment extends BaseMVPFragment<RecommendContract.Presenter> implements RecommendContract.View, View.OnClickListener, BaseQuickAdapter.OnItemChildClickListener {
    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.rv_recommend)
    RecyclerView mRvContent;

    //每个书单详情最多展示几个
    private final int MAX_DETAIL_ITEM_COUNT = 4;
    private RecommendChildAdapter mRecommendChildAdapter;
    private List<BookListDetailSection> data = new ArrayList<>();
    private Map<String, BookListDetailBean> bookListDetailBeanMap = new HashMap<>(20);

    private String tag = "";

    public static Fragment newInstance(String tag) {
        Bundle bundle = new Bundle();
        bundle.putString("tag", tag);
        Fragment fragment = new RecommendChildFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    private void setUpRecyclerView() {
        mRvContent.setLayoutManager(new LinearLayoutManager(getContext()));
        if (getContext() != null) {
            mRvContent.addItemDecoration(new DividerItemDecoration(getContext()));
        }
        mRecommendChildAdapter = new RecommendChildAdapter(R.layout.item_book_list_book_info, R.layout.item_recommend_header, data);
        if (this.tag.equals(getString(R.string.sex_female))) {
            mRecommendChildAdapter.addHeaderView(getLayoutInflater().inflate(R.layout.fragment_recommend_child_header_female, null));
            mRecommendChildAdapter.getHeaderLayout().findViewById(R.id.ll_nav_xiaoyuan).setOnClickListener(this);
            mRecommendChildAdapter.getHeaderLayout().findViewById(R.id.ll_nav_xianyan).setOnClickListener(this);
            mRecommendChildAdapter.getHeaderLayout().findViewById(R.id.ll_nav_guyan).setOnClickListener(this);
            mRecommendChildAdapter.getHeaderLayout().findViewById(R.id.ll_nav_huanyan).setOnClickListener(this);
        } else {
            mRecommendChildAdapter.addHeaderView(getLayoutInflater().inflate(R.layout.fragment_recommend_child_header_male, null));
            mRecommendChildAdapter.getHeaderLayout().findViewById(R.id.ll_nav_xianxia).setOnClickListener(this);
            mRecommendChildAdapter.getHeaderLayout().findViewById(R.id.ll_nav_dushi).setOnClickListener(this);
            mRecommendChildAdapter.getHeaderLayout().findViewById(R.id.ll_nav_xuanhuan).setOnClickListener(this);
            mRecommendChildAdapter.getHeaderLayout().findViewById(R.id.ll_nav_lishi).setOnClickListener(this);
        }
        mRecommendChildAdapter.getHeaderLayout().findViewById(R.id.ll_nav_fenlei).setOnClickListener(this);

        mRecommendChildAdapter.setOnItemChildClickListener(this);
        mRvContent.setAdapter(mRecommendChildAdapter);

    }

    @Override
    public int getContentId() {
        return R.layout.fragment_recommend_child;
    }

    @Override
    protected RecommendContract.Presenter bindPresenter() {
        return new RecommendPresenter();
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {

    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        if (savedInstanceState != null) {
            tag = savedInstanceState.getString("tag");
        }
        if (getArguments() != null) {
            tag = getArguments().getString("tag");
        }
//        refreshLayout.setColorSchemeColors(ThemeStore.accentColor(MApplication.getInstance()));
        if (getContext() != null) {
            refreshLayout.setColorSchemeColors(getContext().getResources().getColor(R.color.colorAccent));
        }
        refreshLayout.setOnRefreshListener(() -> {
            refreshData();
            refreshLayout.setRefreshing(false);
        });
        setUpRecyclerView();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        if (savedInstanceState != null) {
            tag = savedInstanceState.getString("tag");
        }
    }


    /**
     * 首次逻辑操作
     */
    @Override
    protected void startLogic() {
        super.startLogic();
        refreshData();
    }

    public void refreshData() {
        if (mPresenter != null) {
            if (getArguments() != null) {
                tag = getArguments().getString("tag");
            }
            mPresenter.initData(tag);
        }
    }

    @Override
    public void upData(BookListDetailBean bookListDetailBean) {
        if (bookListDetailBean.getBooks().size() == 0) {
            return;
        } else {
            String comment = bookListDetailBean.getBooks().get(0).getComment();
            if (comment == null || comment.trim().equals("")) {
                return;
            }
        }
        bookListDetailBeanMap.put(bookListDetailBean.getTitle(), bookListDetailBean);
        data.add(new BookListDetailSection(true, bookListDetailBean.getTitle()));
        int size = bookListDetailBean.getBooks().size();
        size = size > MAX_DETAIL_ITEM_COUNT ? MAX_DETAIL_ITEM_COUNT : size;
        for (int i = 0; i < size; i++) {
            BookListDetailBean.BooksBean.BookBean bookBean = bookListDetailBean.getBooks().get(i).getBook();
            bookBean.setLongIntro(bookListDetailBean.getBooks().get(i).getComment());
            data.add(new BookListDetailSection(bookBean));
        }
        mRecommendChildAdapter.notifyDataSetChanged();
//        this.data = group;
//        mRecommendChildAdapter.setOnLoadMoreListener();
//        upUI();
    }


    public void upUI() {
//        mRecommendChildAdapter.refreshItems(this.data);
//        mRecommendChildAdapter.s
    }


    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        outState.putString("tag", tag);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        BookSubSortBean bookSubSortBean = new BookSubSortBean();
        if (id == R.id.ll_nav_xianxia) {
            bookSubSortBean.setMajor("仙侠");
            BookCategoryDetailActivity.startActivity(getContext(), "male", bookSubSortBean);

        } else if (id == R.id.ll_nav_dushi) {
            bookSubSortBean.setMajor("都市");
            BookCategoryDetailActivity.startActivity(getContext(), "male", bookSubSortBean);

        } else if (id == R.id.ll_nav_xuanhuan) {
            bookSubSortBean.setMajor("玄幻");
//            bookSubSortBean.setMins(Arrays.asList("东方玄幻", "异界大陆", "异界争霸", "远古神话"));
            BookCategoryDetailActivity.startActivity(getContext(), "male", bookSubSortBean);

        } else if (id == R.id.ll_nav_lishi) {
            bookSubSortBean.setMajor("历史");
            BookCategoryDetailActivity.startActivity(getContext(), "male", bookSubSortBean);

        } else if (id == R.id.ll_nav_xiaoyuan) {
            bookSubSortBean.setMajor("青春校园");
            BookCategoryDetailActivity.startActivity(getContext(), "female", bookSubSortBean);

        } else if (id == R.id.ll_nav_xianyan) {
            bookSubSortBean.setMajor("现代言情");
            BookCategoryDetailActivity.startActivity(getContext(), "female", bookSubSortBean);

        } else if (id == R.id.ll_nav_guyan) {
            bookSubSortBean.setMajor("古代言情");
            BookCategoryDetailActivity.startActivity(getContext(), "female", bookSubSortBean);

        } else if (id == R.id.ll_nav_huanyan) {
            bookSubSortBean.setMajor("玄幻奇幻");
            BookCategoryDetailActivity.startActivity(getContext(), "female", bookSubSortBean);
        } else if (id == R.id.ll_nav_fenlei) {
            startActivity(new Intent(getContext(), BookCategoryActivity.class));
        }
    }


    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        BookListDetailSection section = data.get(position);
        if (section.isHeader) {
            if(view.getId() == R.id.tv_recommend_header_more) {
                Intent intent = new Intent(getContext(), BookRecommendDetailActivity.class);
                intent.putExtra("data", bookListDetailBeanMap.get(section.header));
                intent.putExtra("title", section.header);
                startActivity(intent);
                return;
            }
        }
        String searchKey = section.t.getTitle();
        Intent intent = new Intent(getContext(), SearchActivity.class);
        intent.putExtra("searchKey", searchKey);
        startActivity(intent);
    }
}
